<?php
/***************************************************************************
   Coppermine 1.3.1 for CPG-Dragonfly™
  **************************************************************************
   Port Copyright (c) 2004-2005 CPG Dev Team
   http://dragonflycms.com/
  **************************************************************************
   v1.1 (c) by Grégory Demar http://coppermine.sf.net/
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
****************************************************************************/
if (!defined('CPG_NUKE')) { die("You can't access this file directly..."); }

// Add a picture to an album
function add_picture($aid, $filepath, $filename, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $watermark=false, $src_file=false)
{
	global $db, $CONFIG, $ERROR, $USER_DATA, $PIC_NEED_APPROVAL, $CPG_M_DIR;
	global $username, $raw_ip, $hdr_ip;

	$image = $filepath.$filename;
	$normal = $filepath.$CONFIG['normal_pfx'].$filename;
	$thumb = $filepath.$CONFIG['thumb_pfx'].$filename;
	// $image = $CPG_M_DIR."/albums/$image";
	// $normal = $CPG_M_DIR."/albums/$normal";
	// $thumb = $CPG_M_DIR."/albums/$thumb";
	if (!$src_file) $src_file = $image;
	if (!($imginfo = getimagesize($src_file))) { return false; }

	if (!file_exists($thumb) &&
	  !resize_image($src_file, $imginfo, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_use'])) {
		return false;
	}

	if (max($imginfo[0], $imginfo[1]) > $CONFIG['picture_width'] && $CONFIG['make_intermediate'] && !file_exists($normal) &&
	  !resize_image($src_file, $imginfo, $normal, $CONFIG['picture_width'], $CONFIG['thumb_use'], $watermark)) {
		unlink($thumb);
		return false;
	}

	if (!($imginfo = getimagesize($image))) {
		unlink($normal);
		unlink($thumb);
		return false;
	}
	$image_filesize = filesize($image);
	$total_filesize = $image_filesize + (file_exists($normal) ? filesize($normal) : 0) + filesize($thumb);
	// Test if disk quota exceeded
	if (!GALLERY_ADMIN_MODE && $USER_DATA['group_quota']) {
		$record = $db->uFetchRow("SELECT sum(total_filesize) FROM {$CONFIG['TABLE_PICTURES']}, {$CONFIG['TABLE_ALBUMS']} WHERE  {$CONFIG['TABLE_PICTURES']}.aid = {$CONFIG['TABLE_ALBUMS']}.aid AND category = '".(FIRST_USER_CAT + USER_ID)."'");
		$total_space_used = $record[0];
		if ($total_space_used + $total_filesize > $USER_DATA['group_quota'] << 10) {
			unlink($normal);
			unlink($thumb);
			$msg = strtr(QUOTA_EXCEEDED, array('[quota]' => ($USER_DATA['group_quota']),
                            '[space]' => ($total_space_used >> 10)));
			cpg_die(_ERROR, $msg);
		}
	}
    // Test if picture requires approval
    $approved = intval(GALLERY_ADMIN_MODE || defined('BATCH_MODE') ||
	   (!$USER_DATA['priv_upl_need_approval'] && $category == FIRST_USER_CAT + USER_ID) ||
	   (!$USER_DATA['pub_upl_need_approval']));
	$PIC_NEED_APPROVAL = (!$approved);
	// User ID is not recorded when in admin mode (ie. for batch uploads)
	$user_id = GALLERY_ADMIN_MODE ? 0 : USER_ID;
	$filepath = Fix_Quotes($filepath);
	$filename = Fix_Quotes($filename);
	$result = $db->exec("INSERT INTO {$CONFIG['TABLE_PICTURES']}
		(aid, filepath, filename, filesize, total_filesize, pwidth, pheight, ctime, owner_id, title, caption, keywords, approved, user1, user2, user3, user4, pic_raw_ip, pic_hdr_ip)
		VALUES
		({$aid}, '{$filepath}', '{$filename}', {$image_filesize}, {$total_filesize}, {$imginfo[0]}, {$imginfo[1]}, ".time().", ".USER_ID.", '{$title}', '{$caption}', '{$keywords}', $approved, '{$user1}', '{$user2}', '{$user3}', '{$user4}', {$raw_ip}, {$hdr_ip})");

	speedup_pictures();

	return $result;
}

/**
 * resize_image()
 *
 * Create a file containing a resized image
 *
 * @param  $src_file the source file
 * @param  $dest_file the destination file
 * @param  $new_size the size of the square within which the new image must fit
 * @return 'true' in case of success
*/
function resize_image($src_file, $imginfo, &$dest_file, $new_size, $thumb_use, $watermark=false)
{
	global $CONFIG, $ERROR;

	$img = \Poodle\Image::open($src_file, $CONFIG['thumb_method']);
	if ('ht' == $thumb_use) {
		$img->thumbnailImage(0, $new_size);
	} else if ('wd' == $thumb_use) {
		$img->thumbnailImage($new_size, 0);
	} else {
		$img->thumbnailImage($new_size, $new_size, true);
	}
	$img->setImageCompressionQuality(intval($CONFIG['jpeg_qual']));

	if ($watermark) {
		$img->add_text(array(
			'text'  => $watermark,
//			'x'     => 10,
//			'y'     => 20,
//			'size'  => 12,
//			'color' => '#000000',
//			'font'  => 'includes/poodle/image/fonts/default.ttf',
//			'angle' => 0,
		));
	}

	if (!$img->writeImage($dest_file))
	{
		$ERROR = RESIZE_FAILED;
		return false;
	}

	$dest_file = $img->getImageFilename();
	// Set mode of uploaded picture
	chmod($dest_file, (PHP::$asNobody ? 0666 : 0644));
	return true;
}
